import GameData from "./GameData";
import vv from "./vv";
import v from "./v";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {



    
    
    //起跑线
    @property(cc.Node)
    shaicheqipaoshaichexian: cc.Node = null



    //汽车
    @property(cc.Node)
    shaichecar: cc.Node = null


    //晒车关卡图标
    @property(cc.Node)
    guankatubiao:cc.Node = null


    //倒计时
    @property(cc.Node)
    shaichecountdown: cc.Node = null

    //关卡label
    @property(cc.Node)
    shaicheguankalabel: cc.Node = null


   

    //红灯亮
    @property(cc.Node)
    shaicheredliang: cc.Node = null

    //绿灯亮
    @property(cc.Node)
    shaichergrennliang: cc.Node = null

    //红绿灯底座
    @property(cc.Node)
    shaicherredAndGreenDi: cc.Node = null

    //声音组建
    @property(cc.Node)
    AudioSource:cc.Node = null

    //拖尾效果
    @property(cc.Node)
    shaichetuowei1:cc.Node = null

    @property(cc.Node)
    shaichetuowei2:cc.Node = null
   

 

    

    //绘图
    @property(cc.Node)
    shaichehuitu:cc.Node = null

    //shaichexian
    shaichexian:cc.Graphics



    //背景点击node
    @property(cc.Node)
    bgNode:cc.Node = null



       @property(cc.Node)
       shaichegaojia1:cc.Node = null
       @property(cc.Node)
       shaichegaojia2:cc.Node = null
       @property(cc.Node)
       shaichegaojia3:cc.Node = null
       @property(cc.Node)
       shaichegaojia4:cc.Node = null

       @property(cc.Node)
       shaichegaojia5:cc.Node = null
       @property(cc.Node)
       shaicheggaojia6:cc.Node = null
       @property(cc.Node)
       shaichegaojia7:cc.Node = null
       @property(cc.Node)
       shaichegaojia8:cc.Node = null

       @property(cc.Node)
       shaichegaojia9:cc.Node = null
       @property(cc.Node)
       shaichegaojia10:cc.Node = null
       @property(cc.Node)
       shaichegaojia11:cc.Node = null
       @property(cc.Node)
       shaichegaojia12:cc.Node = null
       @property(cc.Node)
       shaichegaojia13:cc.Node = null
       @property(cc.Node)
       shaichegaojia14:cc.Node = null

       //关卡
       @property(cc.Node)
       guankaPanel:cc.Node = null

       //41的墙
       @property(cc.Node)
       wall41:cc.Node = null

       //起跑灯
       @property(cc.Prefab)
       qipaodeng:cc.Prefab = null

       qipaodengNode:cc.Node


    onLoad() {

        cc.director.getPhysicsManager().enabled = true
        cc.director.getCollisionManager().enabled = true
    }


 
    onEnable()
    {


        GameData.Instance.over = false
        GameData.Instance.isPeng = false
       let luogang = 
       {
           "luogang":"哈哈",
           "luogang1":"哈哈",
           "luogang2":"哈哈",
           "luogang3":"哈哈",
           "luogang4":"哈哈",
           "luogang5":"哈哈"
       }
    }

//画线
shaichehuashaichexian(yuandian,shaichecar,Left)
{
    let luogang = 
    {
        "luogang":"哈哈",
        "luogang1":"哈哈",
        "luogang2":"哈哈",
        "luogang3":"哈哈",
        "luogang4":"哈哈",
        "luogang5":"哈哈"
    }
    this.Stophuashaichexian()
    //画线的起点
    this.shaichexian.moveTo(yuandian.position.x,yuandian.position.y); 
    //画线的终点，就是画到哪里

    if(Left == true)
    {
        this.shaichexian.lineTo(shaichecar.position.x-20,shaichecar.position.y); 
    }
    else 
    {
        this.shaichexian.lineTo(shaichecar.position.x,shaichecar.position.y); 
    }

    this.shaichexian.stroke();  

  
}

 //起跑线向中动画
 shaicheshaicheqipaoshaichexianZhong() {
    let luogang = 
    {
        "luogang":"哈哈",
        "luogang1":"哈哈",
        "luogang2":"哈哈",
        "luogang3":"哈哈",
        "luogang4":"哈哈",
        "luogang5":"哈哈"
    }
    this.shaicheqipaoshaichexian.getComponent(cc.Animation).play('起跑线向中')
}

//起跑线向右动画
shaicheqipaoshaichexianYou() {
    let luogang = 
    {
        "luogang":"哈哈",
        "luogang1":"哈哈",
        "luogang2":"哈哈",
        "luogang3":"哈哈",
        "luogang4":"哈哈",
        "luogang5":"哈哈"
    }
    this.shaicheqipaoshaichexian.getComponent(cc.Animation).play('起跑线向右')
}

//车向上动画
shaichecarShang() {
    let luogang = 
    {
        "luogang":"哈哈",
        "luogang1":"哈哈",
        "luogang2":"哈哈",
        "luogang3":"哈哈",
        "luogang4":"哈哈",
        "luogang5":"哈哈"
    }
    this.shaichecar.getComponent(cc.Animation).play()
}

//shaicelogo弹出动画
shaicelogoTanchu() {
    let luogang = 
    {
        "luogang":"哈哈",
        "luogang1":"哈哈",
        "luogang2":"哈哈",
        "luogang3":"哈哈",
        "luogang4":"哈哈",
        "luogang5":"哈哈"
    }
    //this.shaicelogo.getComponent(cc.Animation).play()
    let shaicelogoaction = cc.scaleTo(0.3, 1.2)
    let shaicelogoaction2 = cc.scaleTo(0.1, 1)
    let shaicelogoaction3 = cc.sequence(shaicelogoaction, shaicelogoaction2)
}

//停止画线
Stophuashaichexian()
{
    let luogang = 
    {
        "luogang":"哈哈",
        "luogang1":"哈哈",
        "luogang2":"哈哈",
        "luogang3":"哈哈",
        "luogang4":"哈哈",
        "luogang5":"哈哈"
    }
    this.shaichexian.clear()
}

    start() {

        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        let url = '汽车/'+GameData.Instance.Onshaichecar+'.png'
        vv.SetImageByLocalUrl(url,this.shaichecar)
        if(GameData.Instance.Onshaichecar == '葱花面包'||GameData.Instance.Onshaichecar == '橘色雄鹿'||GameData.Instance.Onshaichecar == '叉车')
        {
            GameData.Instance.speed = 800
        }
        if(GameData.Instance.Onshaichecar == '小橘牛'||GameData.Instance.Onshaichecar == '救火车'||GameData.Instance.Onshaichecar == '黄卡'||GameData.Instance.Onshaichecar == '韵达电动车')
        {
            GameData.Instance.speed = 850
        }

        if(GameData.Instance.Onshaichecar == '白色幽灵'||GameData.Instance.Onshaichecar == '大脚怪'||GameData.Instance.Onshaichecar == '铲车'||GameData.Instance.Onshaichecar == '韵达电动车')
        {
            GameData.Instance.speed = 900
        }

        if(GameData.Instance.Onshaichecar == '黑影23号'||GameData.Instance.Onshaichecar == '绿影')
        {
            GameData.Instance.speed = 950
        }

        this.shaichecountdown1()

        if(window['wx'])
        {
            window['wx'].showShareMenu()
            window['wx'].onShareAppMessage(function () {
                return {
                  title: GameData.Instance.title,
                  imageUrl: GameData.Instance.image
                }
              })
        }

        v.on('gaojia',()=>
        {
            let luogang = 
            {
                "luogang":"哈哈",
                "luogang1":"哈哈",
                "luogang2":"哈哈",
                "luogang3":"哈哈",
                "luogang4":"哈哈",
                "luogang5":"哈哈"
            }
            this.shaichegaojia4.zIndex = 0
            this.shaichegaojia4.children[0].active = true
            this.shaichegaojia4.children[1].active = true
            this.shaichegaojia1.zIndex = 0
            this.shaichegaojia1.children[0].active = true
            this.shaichegaojia1.children[1].active = true
            this.shaichegaojia2.zIndex = 0
            this.shaichegaojia2.children[0].active = true
            this.shaichegaojia2.children[1].active = true
            this.shaichegaojia3.zIndex = 0
            this.shaichegaojia3.children[0].active = true
            this.shaichegaojia3.children[1].active = true
        })

        v.on('chijinbi',()=>
        {
            let luogang = 
            {
                "luogang":"哈哈",
                "luogang1":"哈哈",
                "luogang2":"哈哈",
                "luogang3":"哈哈",
                "luogang4":"哈哈",
                "luogang5":"哈哈"
            }
            this.schedule(function() {
              let action1 = cc.scaleTo(0.1,1.2)
              let action2 = cc.scaleTo(0.1,1)
              let action = cc.sequence(action1,action2)
              if(this.shaichejinbikuang!=null)
              {
                this.shaichejinbikuang.runAction(action)
              }
            }, 0.1, 5, 0.01);
        })

        this.shaichexian= this.shaichehuitu.getComponent(cc.Graphics)
       v.on('pengzhuang',()=>
       {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
           if(GameData.Instance.maxScore<GameData.Instance.guanka)
           {
                GameData.Instance.maxScore = GameData.Instance.guanka
        let kvdata = [{ key: "maxguanka", value: JSON.stringify(GameData.Instance.maxScore)}]
        if(window['wx'])
        {
            window['wx'].setUserCloudStorage({
                KVDataList: kvdata,
                success: res => {
                    console.log('存储数据成功',res)
                }, fail: res => {
                    console.log('存储数据失败',res)
                }
            })
        }
           }

           if(this.shaichecar!=null)
           {
             this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(0,0)
             this.shaichecar.getComponent(cc.RigidBody).angularVelocity = 0
             console.log(this.shaichecar)
             this.shaichecar.color = cc.Color.BLACK
             this.shaichecar.getChildByName('qihuo').getComponent(cc.ParticleSystem).resetSystem()
           }
       })
        GameData.Instance.piaoyi = false
        //开始点击
        this.bgNode.on(cc.Node.EventType.TOUCH_START, () => {
            let luogang = 
            {
                "luogang":"哈哈",
                "luogang1":"哈哈",
                "luogang2":"哈哈",
                "luogang3":"哈哈",
                "luogang4":"哈哈",
                "luogang5":"哈哈"
            }
            GameData.Instance.onClick = true
            if(GameData.Instance.over == false)
            {
                if(GameData.Instance.onhuashaichexian == true)
                {
                    GameData.Instance.huashaichexian = true
                    GameData.Instance.onClick = true
                }
                if(GameData.Instance.onStay == true)
                {
                    console.log('进入旋转区')
                    GameData.Instance.xuanzhuan = true
                    if(GameData.Instance.shaichevoiceOnOrOff == true)
                    {
                        this.AudioSource.children[0].getComponent(cc.AudioSource).play()
                    }
                  
                    GameData.Instance.piaoyi = true
                    this.shaichecar.getChildByName('weiqi').getComponent(cc.ParticleSystem).resetSystem()
                    if(GameData.Instance.currentdian.getComponent(cc.RopeJoint)!=null&&GameData.Instance.currentdian.getComponent(cc.RopeJoint).enabled==false)
                    {
                        console.log('开始绳子拉')
                        GameData.Instance.currentdian.getChildByName('漂移点1-2').active = true
                      
                   }
                }
            }
           
        })

        

        //松开手指
        this.node.on(cc.Node.EventType.TOUCH_END, () => {

            console.log("gameover",GameData.Instance.over)
            console.log("GameData.Instance.huizheng",GameData.Instance.huizheng)
            let luogang = 
            {
                "luogang":"哈哈",
                "luogang1":"哈哈",
                "luogang2":"哈哈",
                "luogang3":"哈哈",
                "luogang4":"哈哈",
                "luogang5":"哈哈"
            }
            if(GameData.Instance.over == false)
            {
                GameData.Instance.xuanzhuan = false
                GameData.Instance.onClick = false
                GameData.Instance.huashaichexian = false
                this.shaichecar.getChildByName('weiqi').getComponent(cc.ParticleSystem).stopSystem()
                this.Stophuashaichexian()
                this.scheduleOnce(function() {
                    this.shaichetuowei1.getComponent(cc.MotionStreak).enabled = false
                    this.shaichetuowei2.getComponent(cc.MotionStreak).enabled = false
                }, 0.5);
                this.AudioSource.children[0].getComponent(cc.AudioSource).stop()
                GameData.Instance.piaoyi = false
                if(GameData.Instance.currentdian!=null)
                {
                    GameData.Instance.currentdian.getComponent(cc.RopeJoint).enabled = false
                    GameData.Instance.currentdian.getChildByName('漂移点1-2').active = false
                }
                if(GameData.Instance.currentdian!=null)
                {

                    if(GameData.Instance.currentdian.children[2].name == 'left'&&GameData.Instance.huizheng == true)
                    {
                        this.shaichecarTurnLeft()
                        console.log('当前点',GameData.Instance.currentdian.name)
                        if(GameData.Instance.currentdian.name=='1')
                        {
                            let luogang = 
                            {
                                "luogang":"哈哈",
                                "luogang1":"哈哈",
                                "luogang2":"哈哈",
                                "luogang3":"哈哈",
                                "luogang4":"哈哈",
                                "luogang5":"哈哈"
                            }
                            this.wall41.children[0].active = false
                            this.wall41.children[1].active = false

                            this.shaichegaojia1.zIndex = 2
                            this.shaichegaojia1.children[0].active = false
                            this.shaichegaojia1.children[1].active = false
                            this.shaichegaojia2.zIndex = 2
                            this.shaichegaojia2.children[0].active = false
                            this.shaichegaojia2.children[1].active = false
                            this.shaichegaojia3.zIndex = 2
                            this.shaichegaojia3.children[0].active = false
                            this.shaichegaojia3.children[1].active = false
                            this.shaichegaojia4.zIndex = 2
                            this.shaichegaojia4.children[0].active = false
                            this.shaichegaojia4.children[1].active = false
                        }
                        if(GameData.Instance.currentdian.name=='30')
                        {
                            this.shaichegaojia13.zIndex = 0
                        }
                        if(GameData.Instance.currentdian.name=='40')
                        {
                            this.shaichegaojia14.zIndex = 0
                        }
                    }
                    if(GameData.Instance.currentdian.children[2].name == 'right'&&GameData.Instance.huizheng == true)
                    {
                        let luogang = 
                        {
                            "luogang":"哈哈",
                            "luogang1":"哈哈",
                            "luogang2":"哈哈",
                            "luogang3":"哈哈",
                            "luogang4":"哈哈",
                            "luogang5":"哈哈"
                        }
                        this.shaichecarTurnright()
                        if(GameData.Instance.currentdian.name=='18')
                        {
                            this.shaichegaojia8.zIndex = 2
                            this.shaichegaojia8.children[0].active = false
                            this.shaichegaojia8.children[1].active = false
                            this.shaichegaojia9.zIndex = 2
                            this.shaichegaojia9.children[0].active = false
                            this.shaichegaojia9.children[1].active = false
                            this.shaichegaojia10.zIndex = 2
                            this.shaichegaojia10.children[0].active = false
                            this.shaichegaojia10.children[1].active = false
                            this.shaichegaojia11.zIndex = 2
                            this.shaichegaojia11.children[0].active = false
                            this.shaichegaojia11.children[1].active = false


                            this.shaichegaojia5.zIndex = 0
                            this.shaichegaojia5.children[0].active = true
                            this.shaichegaojia5.children[1].active = true
                            this.shaicheggaojia6.zIndex = 0
                            this.shaicheggaojia6.children[0].active = true
                            this.shaicheggaojia6.children[1].active = true
                            this.shaichegaojia7.zIndex = 0
                            this.shaichegaojia7.children[0].active = true
                            this.shaichegaojia7.children[1].active = true
                            this.shaichegaojia12.zIndex = 0
                            this.shaichegaojia12.children[0].active = true
                            this.shaichegaojia12.children[1].active = true
                        }
                        if(GameData.Instance.currentdian.name=='28')
                        {
                            this.shaichegaojia13.zIndex = 2  
                        }
                    }
                    if(GameData.Instance.currentdian.children[2].name == 'top'&&GameData.Instance.huizheng == true)
                    {
                        let luogang = 
                        {
                            "luogang":"哈哈",
                            "luogang1":"哈哈",
                            "luogang2":"哈哈",
                            "luogang3":"哈哈",
                            "luogang4":"哈哈",
                            "luogang5":"哈哈"
                        }
                        this.shaichecarTurnTop()
                      
                        if(GameData.Instance.currentdian.name=='41')
                        {
                            let luogang = 
                            {
                                "luogang":"哈哈",
                                "luogang1":"哈哈",
                                "luogang2":"哈哈",
                                "luogang3":"哈哈",
                                "luogang4":"哈哈",
                                "luogang5":"哈哈"
                            }
                            this.shaichegaojia14.zIndex = 2
                            for(let i = 0;i<this.guankaPanel.childrenCount;i++)
                            {
                              let a:number = (Number)(this.guankaPanel.children[this.guankaPanel.childrenCount-1].getComponent(cc.Label).string)
                              let b = a+1
                            if(i==0)
                            {
                                this.guankaPanel.children[i].getComponent(cc.Label).string = b.toString()
                            }
                            else
                            {
                                let  c = (Number)(this.guankaPanel.children[i-1].getComponent(cc.Label).string)
                                let d = c+1
                                this.guankaPanel.children[i].getComponent(cc.Label).string  = d.toString()
                            }
                            }
                        }
                    }
                    if(GameData.Instance.currentdian.children[2].name == 'down'&&GameData.Instance.huizheng == true)
                    {
                        let luogang = 
                        {
                            "luogang":"哈哈",
                            "luogang1":"哈哈",
                            "luogang2":"哈哈",
                            "luogang3":"哈哈",
                            "luogang4":"哈哈",
                            "luogang5":"哈哈"
                        }
                        this.shaichecarTurnDown()
                        if(GameData.Instance.currentdian.name=='23')
                        {
                            let luogang = 
                            {
                                "luogang":"哈哈",
                                "luogang1":"哈哈",
                                "luogang2":"哈哈",
                                "luogang3":"哈哈",
                                "luogang4":"哈哈",
                                "luogang5":"哈哈"
                            }
                            this.shaichegaojia8.zIndex = 0
                            this.shaichegaojia8.children[0].active = true
                            this.shaichegaojia8.children[1].active = true
                            this.shaichegaojia9.zIndex = 0
                            this.shaichegaojia9.children[0].active = true
                            this.shaichegaojia9.children[1].active = true
                            this.shaichegaojia10.zIndex = 0
                            this.shaichegaojia10.children[0].active = true
                            this.shaichegaojia10.children[1].active = true
                            this.shaichegaojia11.zIndex = 0
                            this.shaichegaojia11.children[0].active = true
                            this.shaichegaojia11.children[1].active = true


                            
                            this.shaichegaojia5.children[0].active = false
                            this.shaichegaojia5.children[1].active = false
                            this.shaicheggaojia6.children[0].active = false
                            this.shaicheggaojia6.children[1].active = false
                            this.shaichegaojia7.children[0].active = false
                            this.shaichegaojia7.children[1].active = false
                            this.shaichegaojia12.children[0].active = false
                            this.shaichegaojia12.children[1].active = false
                        }
                    }
                }
             }
          
        })
    }


    //打开车库
    openCheku()
    {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.shaichevoiceOnOrOff == true)
        {
            this.AudioSource.children[5].getComponent(cc.AudioSource).play()
        }
    }
  

    //汽车向左下角跑
    shaichecarTurnLeftDown() {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.isPeng == false)
        {
            if (this.shaichecar.rotation <=270) {
                console.log('向左下跑')
                let action2 = cc.rotateTo(0.5,-180).easing(cc.easeBackOut())
                this.shaichecar.runAction(action2)
                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(0, -GameData.Instance.speed)
                GameData.Instance.guanka++
            }
        }
    }
    //汽车向右上角跑
    shaichecarTurnRightTop() {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.isPeng == false)
        {
            if (this.shaichecar.rotation <= 180) {
                console.log('向左下跑')
                let action = cc.rotateTo(0.7,0).easing(cc.easeBackOut())
                this.shaichecar.runAction(action)
                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(0, GameData.Instance.speed)
                GameData.Instance.guanka++
            }
        }
    }

    //汽车向右下角跑
    shaichecarTurnRightDown()
    {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.isPeng == false)
        {
            if (this.shaichecar.rotation >= 90) {
                console.log('向左下跑')
                let action = cc.rotateTo(0.7,-150).easing(cc.easeBackOut())
                this.shaichecar.runAction(action)

                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(GameData.Instance.speed-400, -GameData.Instance.speed+200)
                GameData.Instance.guanka++
            }
        }
    }

    //汽车向左跑
    shaichecarTurnLeft() {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.isPeng == false)
        {
            console.log('向左跑',this.shaichecar.rotation)
               // let action1 = cc.rotateTo(0.5,-75).easing(cc.easeBackOut())
                let action = cc.rotateTo(0.5,-90).easing(cc.easeBackOut())
               // let action = cc.sequence(action1,action2)
                this.shaichecar.runAction(action)
                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(-GameData.Instance.speed, 0)
                GameData.Instance.guanka++
        }
    }

    //汽车向右跑
    shaichecarTurnright() {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.isPeng == false)
        {
            if(this.shaichecar.rotation>0&&this.shaichecar.rotation<=180)
            {
                let action = cc.rotateTo(0.7,90).easing(cc.easeBackOut())
                this.shaichecar.runAction(action)
                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(GameData.Instance.speed, 0)
                GameData.Instance.guanka++
            }
        }
    }

    //汽车向左上跑
    shaichecarTurnLeftTop()
    {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.isPeng == false)
        {
            if(this.shaichecar.rotation>-90)
            {
                let action = cc.rotateTo(0.7,-30).easing(cc.easeBackOut())
                this.shaichecar.runAction(action)
                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(-GameData.Instance.speed+400, GameData.Instance.speed-200)
                GameData.Instance.guanka++
            }
        }
    }

    //汽车向上跑
    shaichecarTurnTop() {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.isPeng == false)
        {
                let action = cc.rotateTo(0.7,0).easing(cc.easeBackOut())
                this.shaichecar.runAction(action)
                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(0, GameData.Instance.speed)
                GameData.Instance.guanka++
        }
    }

      //汽车向上跑
      shaichecarTurnDownTop() {
        if(GameData.Instance.isPeng == false)
        {
            if (this.shaichecar.rotation > -180 && this.shaichecar.rotation <= 0) {
                let action = cc.rotateTo(0.7,0).easing(cc.easeBackOut())
                this.shaichecar.runAction(action)
                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(0, GameData.Instance.speed)
                GameData.Instance.guanka++
            }
        }
    }

     //汽车向下跑
     shaichecarTurnDown() {
        if(GameData.Instance.isPeng == false)
        {
            console.log('汽车当前的角度',this.shaichecar.rotation)
                console.log('汽车向下跑')
                let action = cc.rotateTo(0.7,-180).easing(cc.easeBackOut())
                this.shaichecar.runAction(action)
                this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(0, -GameData.Instance.speed)
                GameData.Instance.guanka++
        }
    }

    //倒计时
    shaichecountdown1() {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        let count = 3
        let self = this
        this.schedule(function () {
            self.shaicherredAndGreenDi.active = true
            self.shaicheredliang.opacity = 0
            self.shaichergrennliang.opacity = 0
            self.shaichecountdown.active = true
            self.shaichecountdown.scale = 1
            self.shaichecountdown.opacity = 255
            self.shaichecountdown.getComponent(cc.Label).string = count.toString()
            let action4 = cc.scaleTo(0.5, 3)
            let action5 = cc.fadeOut(1)
            self.shaichecountdown.runAction(action4)
            self.shaichecountdown.runAction(action5)
            if (count>1) {
                if(GameData.Instance.shaichevoiceOnOrOff == true&&count==3)
                {
                    this.AudioSource.children[4].getComponent(cc.AudioSource).play()
                }
                let action6 = cc.fadeIn(0.2)
                self.shaicheredliang.runAction(action6)
                if(GameData.Instance.shaichevoiceOnOrOff == true)
                {
                    this.AudioSource.children[2].getComponent(cc.AudioSource).stop()
                    this.AudioSource.children[2].getComponent(cc.AudioSource).play()
                }
            }
            if (count == 1) {
                let action7 = cc.fadeIn(0.2)
                self.shaichergrennliang.runAction(action7)
                if(GameData.Instance.shaichevoiceOnOrOff == true)
                {
                    this.AudioSource.children[3].getComponent(cc.AudioSource).play()
                }
            }
            count--
            if (count < 0) {
             
                self.shaichecountdown.active = false
                self.shaicherredAndGreenDi.active = false
                this.shaicheqipaoshaichexianYou()
                this.scheduleOnce(function() {
                    // 这里的 this 指向 component
                    this.shaichecar.getComponent(cc.RigidBody).linearVelocity = new cc.Vec2(0, GameData.Instance.speed)
                }, 0.4);
            }
        }, 1, 3, 1);
    }

    //计算两个之间的距离
    nodedistence(a: cc.Vec2, b: cc.Vec2): number {
        let result = Math.abs(
            Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2))
        )
        return result
    }

    //向心力做圆周运动
    shaichexiangxinli(dt)
    {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }

        this.shaichetuowei1.setPosition(new cc.Vec2(this.shaichecar.position.x-25,this.shaichecar.position.y))
        this.shaichetuowei2.setPosition(new cc.Vec2(this.shaichecar.position.x+25,this.shaichecar.position.y))

         let radius= this.nodedistence(this.shaichecar.position,GameData.Instance.currentdian.position);
         let pointDir=this.shaichecar.position.sub(GameData.Instance.currentdian.position);
         let angle=-pointDir.signAngle(cc.v2(1,0))*180/Math.PI;
         let dtDis=GameData.Instance.speed*dt;

         let currentdianDir=0;

         if(GameData.Instance.rotation)
         {
             currentdianDir=1;
         }
         else
         {
             currentdianDir=-1;

         }
         let detalAngle=dtDis/2/radius/Math.PI*360*currentdianDir;
         let currentRadian=(angle+detalAngle)/180*Math.PI;

         let newPos=cc.v2(Math.cos(currentRadian)*radius,Math.sin(currentRadian)*radius).add(GameData.Instance.currentdian.position);
         var targetDirRotation=newPos.sub(this.shaichecar.position).signAngle(cc.v2(0,1))*180/Math.PI;

         this.shaichecar.rotation=targetDirRotation;
         let x = GameData.Instance.speed * Math.sin(this.shaichecar.rotation * Math.PI / 180);
         let y = GameData.Instance.speed * Math.cos(this.shaichecar.rotation * Math.PI / 180);
          this.shaichecar.getComponent(cc.RigidBody).linearVelocity = cc.v2(x, y)
        let dir = this.shaichecar.position.sub(GameData.Instance.currentdian.position);
        let unit = dir.normalize();
        let force = this.shaichecar.getComponent(cc.RigidBody).getMass() * GameData.Instance.speed * GameData.Instance.speed / dir.mag();
       
        this.shaichecar.getComponent(cc.RigidBody).applyForceToCenter(unit.mul(-force), true);
        if(GameData.Instance.rotation == true)
        {
            this.shaichecar.rotation -= GameData.Instance.speed / dir.mag() * dt * 180 / Math.PI ;
            this.shaichehuashaichexian(GameData.Instance.currentdian,this.shaichecar,true)
        }
        else
        {
            this.shaichecar.rotation += GameData.Instance.speed / dir.mag() * dt * 180 / Math.PI ; 
            this.shaichehuashaichexian(GameData.Instance.currentdian,this.shaichecar,false)
        }

        this.shaichetuowei1.getComponent(cc.MotionStreak).enabled = true
        this.shaichetuowei2.getComponent(cc.MotionStreak).enabled = true
        
    }

    update(dt) {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
        if(GameData.Instance.over == false)
        {
            if(this.shaichecar != null && GameData.Instance.xuanzhuan == true)
            {
                if(GameData.Instance.currentdian.name == '8')
                {
                        this.shaichegaojia4.zIndex = 0
                        this.shaichegaojia4.children[0].active = true
                        this.shaichegaojia4.children[1].active = true
                        this.shaichegaojia1.zIndex = 0
                        this.shaichegaojia1.children[0].active = true
                        this.shaichegaojia1.children[1].active = true
                        this.shaichegaojia2.zIndex = 0
                        this.shaichegaojia2.children[0].active = true
                        this.shaichegaojia2.children[1].active = true
                        this.shaichegaojia3.zIndex = 0
                        this.shaichegaojia3.children[0].active = true
                        this.shaichegaojia3.children[1].active = true
                }

                if(GameData.Instance.currentdian.name == '41')
                {
                    this.wall41.children[0].active = true
                    this.wall41.children[1].active = true
                }
                this.shaichexiangxinli(dt)
            }
            else 

            if(GameData.Instance.onClick==true&&GameData.Instance.onStay==true)
            {
              
                this.shaichexiangxinli(dt)
            }
        }

    }
}